<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <!-- 顶部提示 新增用户按钮 -->
        <div class="top" style="height: 52px">
          <span style="font-size: 12px; color: pink"
            >说明：目前支持学科和关键字条件筛选</span
          >
          <el-button
            class="filter-item fr"
            size="small"
            style="margin-left: 10px"
            type="success"
            icon="el-icon-edit"
            @click="addConfirm"
            >{{ $t('table.addUser') }}</el-button
          >
        </div>
        <!-- 表格 -->
        <el-form
          :model="form"
          ref="formABC"

          label-position="right"
          label-width="80px"
        >
          <!-- 第一行 -->
          <el-row>
            <el-col :span="6">
              <el-form-item label="学科" prop="subjectID" label-width="80px">
                <el-select
                  v-model="form.subjectID"
                  placeholder="请选择"
                  style="width: 100%"
                  size="small"
                  @change="subjectChange"
                >
                  <el-option
                    v-for="item in SubjectList"
                    :key="item.value"
                    :value="item.value"
                    :label="item.label"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="二级目录" prop="towSubjectID"
              label-width="80px">
                <el-select
                  v-model="form.towSubjectID"
                  placeholder="请选择"
                  style="width: 100%"
                  size="small"
                >
                  <el-option
                    v-for="item in towSubjectList"
                    :key="item.id"
                    :value="item.value"
                    :label="item.label"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标签" prop="tags" label-width="80px">
                <el-select
                  v-model="form.tags"
                  placeholder="请选择"
                  style="width: 100%"
                  size="small"
                >
                  <el-option
                    v-for="(item, index) in tagsList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="关键字" prop="keyword" label-width="80px">
                <el-input
                  v-model="form.keyword"
                  size="small"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第二行 -->
          <el-row>
             <el-col :span="6">
            <el-form-item
              label="试题类型"
              label-width="80px"
              prop="questionType"
            >
              <el-select
                v-model="form.questionType"
                placeholder="请选择"
                style="width: 100%"
                size="small"
              >
                <el-option
                  v-for="item in questionType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
             </el-col>
             <el-col :span="6">
            <el-form-item label="难度" label-width="80px" prop="difficulty">
              <el-select
                v-model="form.difficulty"
                placeholder="请选择"
               style="width: 100%"
                size="small"
              >
                <el-option
                  v-for="item in degree"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
             </el-col>
             <el-col :span="6">
            <el-form-item label="方向" label-width="80px" prop="direction">
              <el-select
                v-model="form.direction"
                placeholder="请选择"
               style="width: 100%"
                size="small"
              >
                <el-option
                  v-for="(item, index) in direction"
                  :key="index"
                  :value="item"
                  :label="item"
                ></el-option>
              </el-select>
            </el-form-item>
            </el-col>
            <el-col :span="6">
            <el-form-item label="录入人" label-width="80px" prop="creatorID">
              <el-select
                v-model="form.creatorID"
                placeholder="请选择"
               style="width: 100%"
                size="small"
              >
                <el-option
                  v-for="item in creators"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
           </el-col>
          </el-row>
          <!-- 第三行 -->
          <el-row>
            <el-col :span="6">
            <el-form-item label="题目备注" label-width="80px" prop="remarks">
              <el-input
                v-model="form.remarks"
               style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
            </el-col>
            <el-col :span="6">
            <el-form-item label="企业简称" label-width="80px" prop="shortName">
              <el-input
                v-model="form.shortName"
               style="width: 100%"
                size="small"
              ></el-input>
            </el-form-item>
            </el-col>
            <el-col :span="6">
            <el-form-item label="城市" >
              <el-select
                v-model="form.province"
                placeholder="请选择"
                style="width:48%;margin-right:6px"
                size="small"
                @change="provincesChange"
              >
                <el-option
                  v-for="(item, index) in provinces"
                  :key="index"
                  :label="item"
                  :value="item"
                >
                </el-option>
              </el-select>
              <el-select
                v-model="form.city"
                placeholder="请选择"
               style="width:48%"
                size="small"
              >
                <el-option
                  v-for="item in citys"
                  :key="item"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select>
            </el-form-item>
            </el-col>
            <el-col :span="6">
            <el-form-item >
              <el-row type="flex" justify="end">
                <el-button size="small" @click="clearForm">清除</el-button>
                <el-button type="primary" size="small" @click="searchHandle"
                  >搜索</el-button
                >
              </el-row>
            </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-tabs type="card" @tab-click="tabClickFn">
          <el-tab-pane label="全部"></el-tab-pane>
          <el-tab-pane label="待审核"></el-tab-pane>
          <el-tab-pane label="已审核"></el-tab-pane>
          <el-tab-pane label="已拒绝"></el-tab-pane>
        </el-tabs>
        <div
          style="
            background-color: #f4f4f5;
            padding: 11px;
            font-size: 12px;
            color: #909399;
          "
        >
          <span class="el-icon-info" style="color: #909399"
            >&nbsp;&nbsp;数据一共 {{ total }}条</span
          >
        </div>
        <el-table :data="choiceData" style="width: 100%">
          <el-table-column
            prop="number"
            label="试题编号"
            width="150"
          ></el-table-column>
          <el-table-column prop="subjectID" label="学科">
            <template #default="{ row }">
              <span>{{ subjectListShow(row.subjectID) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="catalogID" label="目录">
            <template #default="{ row }">
              <span>{{ row.catalog }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="questionType" label="题型">
            <template #default="{ row }">
              <span>{{
                row.questionType
                  ? questionType.find(item => item.value === +row.questionType)
                      .label
                  : ''
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="question" label="题干" width="300">
            <template #default="{ row }">
              <!-- // eslint-disable-next-line vue/no-parsing-error -->
              <span v-text="tiganFn(row)"></span>
            </template>
          </el-table-column>
          <el-table-column prop="addDate" label="录入时间" width="200">
            <template #default="{ row }">
              <span>{{ timeShow(row.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="difficulty" label="难度">
            <template #default="{ row }">
              <span>{{
                row.difficulty
                  ? degree.find(item => item.value === +row.difficulty).label
                  : '无'
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="creator"
            label="录入人"
            width="100"
          ></el-table-column>
          <el-table-column prop="chkState" label="审核状态">
            <template #default="{ row }">
              <!-- <span>{{ row.chkState ? chkType.find(item=> item.value === row.chkState).label + row.chkState : '无' +  row.chkState}}</span> -->
              <!-- <span>{{ chkType.find(item => item.value === +row.chkState).label }}</span> -->
              <span>{{
                row.chkState === 1
                  ? '已审核'
                  : row.chkState === 2
                  ? '已拒绝'
                  : '待审核'
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="chkRemarks"
            label="审核意见"
            width="150"
          ></el-table-column>
          <el-table-column
            prop="chkUser"
            label="审核人"
            width="120"
          ></el-table-column>
          <el-table-column prop="publishState" label="发布状态">
            <template #default="{ row }">
              <span>{{
                row.publishState === 0
                  ? '待发布'
                  : row.publishState === 1
                  ? '已发布'
                  : '错误'
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="220" fixed="right">
            <template #default="{ row }">
              <el-button type="text" @click="previewClick(row.id)"
                >预览</el-button
              >
              <el-button
                type="text"
                :disabled="!!row.chkRemarks"
                @click="verifyClick(row.id)"
                >审核</el-button
              >
              <el-button
                type="text"
                :disabled="row.publishState === 1"
                @click="handleChange(row)"
                >修改</el-button
              >
              <el-button
                type="text"
                @click="publishStateClick(row.publishState, row.id, row)"
                >{{ row.publishState === 0 ? '上架' : '下架' }}</el-button
              >
              <!-- row.publishState === 0 ? '上架' : '下架' -->
              <el-button
                type="text"
                :disabled="row.publishState === 1"
                @click="delSubject(row, row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-row
          style="height: 50px; margin-top: 20px"
          type="flex"
          align="middle"
          justify="end"
        >
          <el-pagination
            background
            :page-sizes="[5, 10, 20, 50]"
            :page-size="form.pagesize"
            layout=" sizes, prev, pager, next, jumper"
            :total="total"
            @current-change="currentChange"
            @size-change="sizeChang"
          >
          </el-pagination>
        </el-row>
      </el-card>
      <questionsAdd
        :showDialog="isShowDialog"
        @handleClose="closeDialog"
        ref="refView"
      ></questionsAdd>
      <el-dialog
        title="题目审核"
        width="21%"
        :visible="dialogShow"
        @close="close"
      >
        <el-form>
          <el-form-iten>
            <el-radio-group v-model="verifyForm.chkState">
              <el-radio :label="1">通过</el-radio>
              <el-radio :label="2">拒绝</el-radio>
            </el-radio-group>
          </el-form-iten>
          <el-form-item style="margin-top: 20px">
            <el-input
              v-model="verifyForm.chkRemarks"
              type="textarea"
              placeholder="请输入审核意见"
            ></el-input>
          </el-form-item>
          <el-row style="margin-top: 60px" type="flex" justify="end">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="verifySubmit">确认</el-button>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  difficulty,
  publishType,
  chkType,
  questionType,
  direction
} from '@/api/hmmm/constants.js'
import {
  choice,
  choiceCheck,
  choicePublish,
  remove
} from '@/api/hmmm/questions.js'
import { provinces, citys } from '@/api/hmmm/citys.js'
import { simple } from '@/api/hmmm/subjects.js'
import { simple as towDirectorysList } from '@/api/hmmm/tags.js'
import { simple as handleLabelList } from '@/api/hmmm/directorys.js'
// handleLabelList
// 引入预览弹层组件
import dayjs from 'dayjs'
import questionsAdd from '../components/questions-add.vue'
export default {
  props: {
    value: {
      default: ''
    }
  },
  components: {
    questionsAdd
  },
  data () {
    return {
      id: null,
      verifyForm: {
        chkRemarks: '',
        id: null,
        chkState: 1
      },
      degree: difficulty,
      publishType,
      chkType,
      questionType,
      direction,
      provinces: provinces(),
      citys: [],
      dialogShow: false,
      form: {
        province: '',
        city: '',
        publishState: null,
        questionType: null,
        chkState: null,
        difficulty: null,
        remarks: '',
        shortName: '',
        keyword: '',
        subjectID: null,
        towSubjectID: null,
        creatorID: null,
        page: 1,
        pagesize: 5,
        tags: null,
        direction: '',
        id: null
      },
      tagsList: [],
      total: null,
      choiceData: [],
      SubjectList: [],
      towSubjectList: [],
      isShowDialog: false,
      timer: null,
      creators: [
        {
          label: '超级管理员',
          value: 2
        },
        {
          label: '录入管理员',
          value: 3
        }
      ]
    }
  },
  created () {
    this.getChoiceList()
    this.getSubjectList()
  },
  methods: {
    // 获取精选题库渲染页面
    async getChoiceList () {
      const { data } = await choice(this.form)
      this.choiceData = data.items
      this.total = data.counts
    },
    // 学科列表
    async getSubjectList () {
      const { data } = await simple()
      this.SubjectList = data
    },
    // 关闭审核弹层
    close () {
      this.verifyForm.chkRemarks = ''
      this.verifyForm.id = null
      this.verifyForm.chkState = 1
      this.dialogShow = false
    },
    // 删除精选数据
    delSubject (row, id) {
      this.$confirm('您确认要删除这道题目吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        if (this.choiceData.length === 1 && this.form.page > 1) {
          this.form.page = this.form.page - 1
          this.form.id = id
        }
        await remove(row)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.getChoiceList()
      })
    },
    // 切换上下架状态
    publishStateClick (state, id, row) {
      this.$confirm(`您确认${state ? '下' : '上'}架这道题目吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // console.log(row)
        await choicePublish({
          id,
          publishState: state ? 0 : 1
        })
        this.getChoiceList()
        this.$message({
          type: 'success',
          message: `${!state ? '上' : '下'}架成功!`
        })
      })
    },
    // 根据审核状态切换页面数据
    tabClickFn (tab) {
      if (+tab.index === 0) {
        this.form.chkState = null
      } else if (+tab.index === 1) {
        this.form.chkState = 0
      } else if (+tab.index === 2) {
        this.form.chkState = 1
      } else if (+tab.index === 3) {
        this.form.chkState = 2
      }
      // console.log(this.form.chkState)
      this.getChoiceList()
    },
    // 地域联动
    provincesChange (e) {
      this.form.city = ''
      this.citys = citys(e)
    },
    // 学科联动
    async subjectChange (id) {
      this.form.towSubjectID = null
      this.form.tags = null
      const { data } = await towDirectorysList({ subjectID: id })
      const res = await handleLabelList({ subjectID: id })
      this.tagsList = data
      this.towSubjectList = res.data
      console.log(this.towSubjectList, this.tagsList)
    },
    // 切换题干
    tiganFn (row) {
      // console.log(row.question.replace(/<[^>]+>/g, ''))
      return row.question.replace(/<[^>]+>/g, '')
    },
    // 详情预览
    previewClick (id) {
      this.isShowDialog = true
      this.$refs.refView.detailList(id)
    },
    closeDialog () {
      this.isShowDialog = false
    },
    // 新增页面
    addConfirm () {
      this.$router.push('/questions/new')
    },
    // 编辑
    handleChange (row) {
      this.$router.push({ path: '/questions/new/', query: { id: row.id } })
      // this.$router.push(`/questions/new/${row.id}`)
    },
    // 审核
    verifyClick (id) {
      this.dialogShow = true
      this.verifyForm.id = id
    },
    // 确认审核
    async verifySubmit () {
      await choiceCheck(this.verifyForm)
      this.$message.success('审核成功')
      this.close()
      this.getChoiceList()
    },
    // 切换显示页面
    currentChange (val) {
      this.form.page = val
      this.getChoiceList()
    },
    // 切换页面显示条数
    sizeChang (val) {
      this.form.pagesize = val
      this.getChoiceList()
    },
    // 搜索
    searchHandle () {
      // 防抖 加不加待定
      // clearTimeout(this.timer)
      // this.timer = setTimeout(() => {
      // }, 500)
      this.form.page = 1
      this.getChoiceList()
    },
    clearForm () {
      // 清空
      this.$refs.formABC.resetFields()
      this.form.province = ''
      this.citys = []
      this.form.city = ''
      this.form.towSubjectID = null
      this.form.tags = null
      this.towSubjectList = []
      this.tagsList = []
      this.getChoiceList()
      // this.$refs.questionsChoiceForm.resetFields()
      // this.$refs.questionsChoiceForm.validate((ok) => {
      //   if (ok) {
      //   }
      // })
      // this.form.subjectID = null
      // this.form.towSubjectID = null
      // this.form.tags = null
      // this.form.keyword = ''
      // this.form.questionType = null
      // this.form.difficulty = null
      // this.form.direction = ''
      // this.form.creatorID = null
      // this.form.remarks = ''
      // this.form.shortName = ''
      // this.form.province = ''
      // this.form.city = ''
      // this.towSubjectList = []
      // this.tagsList = []
      // this.citys = []
    },
    // 学科判断显示列表
    subjectListShow (id) {
      return this.SubjectList.find(item => item.value === id).label
    },
    // async catalogue (row) {
    //   console.log(row)
    //   const { data } = await handleLabelList({ subjectID: row.subjectID })
    //   // const e = data.filter(item => item.value === row.catalogID)[0]
    //   console.log(data)
    //   // return e
    // }

    // 修改时间显示
    timeShow (day) {
      return dayjs(day).format('YYYY-mm-DD HH:mm:ss')
    }
  }
}
</script>

<style lang="scss" scoped></style>
